package com.yuda.operationcenter.controller;

import com.github.pagehelper.PageHelper;
import com.yuda.common.core.utils.StringUtils;
import com.yuda.common.core.utils.poi.ExcelUtil;
import com.yuda.common.core.utils.sql.SqlUtil;
import com.yuda.common.core.web.page.PageDomain;
import com.yuda.common.core.web.page.TableSupport;
import com.yuda.common.security.annotation.PreAuthorize;
import com.yuda.operationcenter.domain.OperSite;
import com.yuda.operationcenter.domain.OperSiteParam;
import com.yuda.operationcenter.domain.OperTake;
import com.yuda.operationcenter.domain.OperTakeParam;
import com.yuda.operationcenter.dto.Result;
import com.yuda.operationcenter.mapper.OperTakeMapper;
import com.yuda.operationcenter.service.OperTakeService;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;

@RestController
public class OperTakeController {
    @Autowired
    OperTakeService operTakeService;

    @PreAuthorize(hasPermi = "gwoper:apply:query")
    @GetMapping("/select/take")
    @ApiOperation("参考资质申请查询接口")
    public List<OperTake> queryTake(OperTakeParam param) {
        startPage();
        System.out.println("----->>>>>>>>> " + param.getApplyTime());
        List<OperTake> operTakes = operTakeService.selectTake(param);
        return operTakes;
    }

    @PreAuthorize(hasPermi = "gwoper:apply:remove")
    @PostMapping("/deleteTake/{id}")
    @ApiOperation("删除参考资质申请接口")
    public Object deleteTakeById(@ApiParam("被删除用户的id") @PathVariable("id") Integer id) {
        int count = operTakeService.deleteTake(id);
        if (count > 0) {
            return new Result().successOperation(count);
        }
        return new Result().failedOperation();
    }

    @PreAuthorize(hasPermi = "gwoper:apply:remove")
    @PostMapping("/deleteTakeMore/{ids}")
    @ApiOperation("批量删除考点资质申请接口")
    public Object deleteTakeMore(@PathVariable("ids") Integer[] ids) {
        int count = operTakeService.deleteTakeMore(ids);
        if (count > 0) {
            return new Result().successOperation(count);
        }
        return new Result().failedOperation();
    }

    @PreAuthorize(hasPermi = "gwoper:apply:export")
    @GetMapping("/exportTake")
    @ApiOperation("导出参考资质申请信息Excel")
    public void exportTake(HttpServletResponse response, OperTakeParam param) throws IOException {
        List<OperTake> operTakes = operTakeService.selectTake(param);
        ExcelUtil<OperTake> excelUtil = new ExcelUtil<>(OperTake.class);
        excelUtil.exportExcel(response, operTakes, "参考资质申请信息表");

    }

    /**
     * 设置请求分页数据
     */
    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
